import type { App } from "vue";

export default async (app: App) => {
  // 放宽类型：允许模块可能有 default 导出
  const pluginList = import.meta.glob<{
    default?: (app: App) => Promise<Record<string, any>> // 用 ? 标记为可选
  }>("./**/index.ts", { eager: true });

  const instance: Record<string, any> = {};

  Object.entries(pluginList).forEach(async ([path, module]) => {
    try {
      // 检查 default 是否存在且为函数
      if (module.default && typeof module.default === "function") {
        const pluginExports = await module.default(app);
        Object.assign(instance, pluginExports);
      } else {
        console.warn(`插件 ${path} 缺少有效的默认导出（需导出函数）`);
      }
    } catch (error) {
      console.error(`加载插件 ${path} 失败:`, error);
    }
  });

  return instance;
};